package com.ruoyi.teach.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.teach.mapper.TeachBookContentMapper;
import com.ruoyi.teach.mapper.TeachBookNodeMapper;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.teach.domain.TeachBookContent;
import com.ruoyi.teach.domain.TeachBookNode;
import com.ruoyi.teach.service.ITeachBookNodeService;

/**
 * 教材章节Service业务层处理
 * 
 * @author yepanpan
 * @date 2021-03-15
 */
@Service
public class TeachBookNodeServiceImpl implements ITeachBookNodeService 
{
    @Autowired
    private TeachBookContentMapper teachBookContentMapper;
    @Autowired
    private TeachBookNodeMapper teachBookNodeMapper;

    /**
     * 查询教材章节
     * 
     * @param id 教材章节ID
     * @return 教材章节
     */
    @Override
    public TeachBookNode selectTeachBookNodeById(Long id)
    {
        return teachBookNodeMapper.selectTeachBookNodeById(id);
    }

    /**
     * 查询教材章节列表
     * 
     * @param teachBookNode 教材章节
     * @return 教材章节
     */
    @Override
    public List<TeachBookNode> selectTeachBookNodeList(TeachBookNode teachBookNode)
    {
        return teachBookNodeMapper.selectTeachBookNodeList(teachBookNode);
    }

    /**
     * 新增教材章节
     * 
     * @param teachBookNode 教材章节
     * @return 结果
     */
    @Override
    public int insertTeachBookNode(TeachBookNode teachBookNode)
    {
    	int count = 0;
    	TeachBookContent content = new TeachBookContent();
    	content.setBookId(teachBookNode.getBookId());
    	content.setComment("内容说明");
    	content.setContent("默认内容");
    	content.setIsCurrent("Y");
    	content.setNodeId(teachBookNode.getId());
        count += teachBookNodeMapper.insertTeachBookNode(teachBookNode);
        
        return count;
    }

    /**
     * 修改教材章节
     * 
     * @param teachBookNode 教材章节
     * @return 结果
     */
    @Override
    public int updateTeachBookNode(TeachBookNode teachBookNode)
    {
        return teachBookNodeMapper.updateTeachBookNode(teachBookNode);
    }

    /**
     * 批量删除教材章节
     * 
     * @param ids 需要删除的教材章节ID
     * @return 结果
     */
    @Override
    public int deleteTeachBookNodeByIds(Long[] ids)
    {
        return teachBookNodeMapper.deleteTeachBookNodeByIds(ids);
    }

    /**
     * 删除教材章节信息
     * 
     * @param id 教材章节ID
     * @return 结果
     */
    @Override
    public int deleteTeachBookNodeById(Long id)
    {
        return teachBookNodeMapper.deleteTeachBookNodeById(id);
    }
    

    /**
     * 查询教材章节树形列表
     * 
     * @param bookId 教材ID
     * @return 教材章节集合
     */
    @Override
    public List<TreeSelect> selectTeachBookNodeTree(Long bookId){
    	TeachBookNode nc = new TeachBookNode();
    	nc.setBookId(bookId);
        List<TeachBookNode> list = teachBookNodeMapper.selectTeachBookNodeList(nc);
        return buildNodeTree(list, 0l);
    }
    
    private List<TreeSelect> buildNodeTree(List<TeachBookNode> list, Long pid){
    	List<TreeSelect> tree = new ArrayList<>();
    	for(TeachBookNode node:list) {
    		if(!node.getPid().equals(pid)) continue;
    		
    		TreeSelect t = new TreeSelect();
    		t.setId(node.getId());
    		t.setLabel(node.getTitle());
    		
    		List<TreeSelect> children = buildNodeTree(list, node.getId());
    		if(children != null && children.size() > 0) {
    			t.setChildren(children);
    		}
    		
    		tree.add(t);
    	}
    	return tree;
    }
}
